<?php
header("content-type:text/html;charset=utf-8");
//数据库连接
$dbhost = 'localhost:3306';  // mysql服务器主机地址
$dbuser = 'root';            // mysql用户名
$dbpass = '';          // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Could not connect: ' . mysqli_error());
}

mysqli_select_db( $conn, 'tmp' );  //选择数据库
$class_sl = $_GET['class'];

$sql = "SELECT id, name, class FROM student";


$retval = mysqli_query( $conn, $sql); 

if(!$retval )
{
    die('无法查询学生: ' . mysqli_error($conn));
}

$str = "<tr><td>序号</td><td>学号</td><td>姓名</td><td>班级</td></tr>";
$page_num = $_GET['page_input'];

$row_cnt = 0;
$page_cnt = 1;
$class_list_cnt = 0;
//获取都有哪些班级

while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC) )
{
    $ishave = 0;
    for($cnt = 0;$cnt<$class_list_cnt;$cnt ++)
    {
        if($class_list[$cnt] == $row['class'])
        {
            $ishave = 1;
        }
    }
    if($ishave == 0)
    {
        $class_list[$class_list_cnt] = $row['class'];
        $class_list_cnt ++;
    }
    
}
$str_class = "<option>选择班级</option><option>全部</option>";
for($cnt = 0;$cnt<$class_list_cnt;$cnt ++)
{
    $str_class = $str_class."<option>$class_list[$cnt]</option>";
}

if($class_sl != '选择班级' && $class_sl != "全部" && $class_sl != null)
{
    $sql = "SELECT id, name, class FROM student WHERE class = '$class_sl'" ;
}
else
{
    $sql = "SELECT id, name, class FROM student";
}

//显示学生信息
$retval = mysqli_query( $conn, $sql); 

$rowsnum = (int)(mysqli_num_rows($retval)/15)+((mysqli_num_rows($retval)%15)?1:0);
if($page_num>$rowsnum)
{
    $page_num = $rowsnum;
}
$st_index = 0;
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC) )
{
    $st_index ++;
    if($page_cnt == $page_num)
    {
        if($row['class'] == $class_sl||$class_sl==""||$class_sl=="全部"||$class_sl=="选择班级")
        $str = $str . "<tr><td>{$st_index}</td><td>{$row['id']}</td><td>{$row['name']}</td><td>{$row['class']}</td></tr>";
    }
    if($row_cnt<14)
    {
        $row_cnt ++;
    }
    else
    {
        $page_cnt ++;
        $row_cnt = 0;
    }
}
$str = $str . "<tr><td>共 $rowsnum 页 </td><td>$class_sl</td></tr>";
//echo "hello word";

echo <<<STR
<script>
  window.parent.document.getElementById('st_tb').innerHTML = "$str";
  if(window.parent.document.getElementById('class_indexid').value == 0)
  {
  window.parent.document.getElementById('sl_id').innerHTML = "$str_class";
  window.parent.document.getElementById('class_indexid').value = 1;
  }
  window.parent.document.getElementById('page_nums').value = $rowsnum;
</script>
STR;
?>